package com.fsxgt.datagrid.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.core.page.PageParam;
import com.fsxgt.datagrid.core.utils.ReturnT;
import com.fsxgt.datagrid.sys.bo.LoginBO;
import com.fsxgt.datagrid.sys.bo.SysUserBO;
import com.fsxgt.datagrid.sys.service.impl.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "用户管理", value = "SysUserController")
@RequestMapping(produces = "application/json; charset=UTF-8",path = "/sysUser")
@RestController
public class SysUserController extends BaseController {

	@Autowired
	private SysUserService sysUserService;


	@ApiOperation(hidden = true, value = "新增用户", notes = "")
	@RequestMapping(path = "/addSysUser", method = RequestMethod.POST)
	public ReturnT addSysUser(@RequestBody SysUserBO sysUserBO, HttpServletRequest request) throws DataGridException {

		sysUserService.addSysUser(sysUserBO);

		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "获取主题", notes = "")
	@RequestMapping(path = "/findSubject", method = RequestMethod.POST)
	public ReturnT findSubject(@RequestBody SysUserBO sysUserBO, HttpServletRequest request) throws DataGridException {

		List list=sysUserService.findSubject();

		return new ReturnT(list);

	}
	
	@ApiOperation(hidden = true, value = "修改用户", notes = "")
	@RequestMapping(path = "/modifySysUser", method = RequestMethod.POST)
	public ReturnT modifySysUser(@RequestBody SysUserBO sysUserBO, HttpServletRequest request) throws DataGridException {

		sysUserService.modifySysUser(sysUserBO);

		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "重置密码", notes = "")
	@RequestMapping(path = "/resetPassword", method = RequestMethod.POST)
	public ReturnT resetPasswork(@RequestBody SysUserBO sysUserBO, HttpServletRequest request)   {
		
		sysUserService.resetPassword(sysUserBO);
		
		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "修改密码", notes = "")
	@RequestMapping(path = "/modifyPassword", method = RequestMethod.POST)
	public ReturnT modifyPasswork(@RequestBody SysUserBO sysUserBO, HttpServletRequest request) throws DataGridException   {
		
		
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		LoginBO loginBO = (LoginBO) authentication.getPrincipal();

		
		sysUserService.modifyPassword(loginBO.getId(),sysUserBO.getOldpassword(),sysUserBO.getNewpassword());
		
		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "获取用户", notes = "")
	@RequestMapping(path = "/getSysUser", method = RequestMethod.POST)
	public ReturnT getSysUser(@RequestBody SysUserBO sysUserBO, HttpServletRequest request)  {

		return new ReturnT(sysUserService.getSysUser(sysUserBO.getId()));

	}
	
	@ApiOperation(hidden = true, value = "删除用户", notes = "")
	@RequestMapping(path = "/deleteSysUser", method = RequestMethod.POST)
	public ReturnT deleteSysUser(@RequestBody SysUserBO sysUserBO, HttpServletRequest request)  {

		sysUserService.deleteSysUser(sysUserBO.getId());

		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "分页查询", notes = "")
	@RequestMapping(path = "/findSysUserPage", method = RequestMethod.POST)
    public ReturnT findSysUserPage(@RequestBody PageParam<SysUserBO> pageParam) {
		
		PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
		
		List<SysUserBO> list=sysUserService.findSysUserPage(pageParam.getParam());
		
		PageInfo<SysUserBO> pageInfo = new PageInfo<>(list);
		
		
        return new ReturnT(pageInfo);
    }
    

	

}